package com.kakaxi.mybilibili.module.home.find.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.FindCenterRecyAdapter;
import com.kakaxi.mybilibili.base.NavigationActivity;
import com.kakaxi.mybilibili.bean.FindCenterItemBean;
import com.kakaxi.mybilibili.module.all.WebViewActivity;
import com.kakaxi.mybilibili.module.home.find.presenter.FindCenterPresenter;
import com.kakaxi.mybilibili.module.home.find.presenter.IFindCenterPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 发现中心活动类
 * Created by kakaxi on 16/11/26.
 */

public class FindCenterActivity extends NavigationActivity implements IFindCenterView {

    @BindView(R.id.srl_common_activity)
    SwipeRefreshLayout swipeRefreshLayout;

    @BindView(R.id.recv_common_activity)
    RecyclerView recyclerView;

    /** 中心类型参数名 **/
    public static final String EXTRA_CENTER_TYPE = "type";

    /** 话题中心 **/
    public static final int TYPE_TOPIC = 0;
    /** 活动中心 **/
    public static final int TYPE_EVENT = 1;

    /** 类型 **/
    private int mType;

    private IFindCenterPresenter mPresenter;

    private FindCenterRecyAdapter mAdapter;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_common_recycler;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refreshCenterData(mType);
            }
        });

        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int pos) {
                Bundle bundle = new Bundle();
                bundle.putString(WebViewActivity.EXTRA_URL, mAdapter.getItem(pos).getLink());
                goActivity(WebViewActivity.class, bundle);
            }
        });

    }

    @Override
    protected void initData() {
        mPresenter = new FindCenterPresenter(this);

        mType = getIntent().getIntExtra(EXTRA_CENTER_TYPE, -1);
        if (mType == -1) {
            toastHandler("缺少参数");
            finish();
            return;
        }

        if (mType == TYPE_TOPIC) {
            setTitle("话题中心");
        } else if (mType == TYPE_EVENT){
            setTitle("活动中心");
        }

        mPresenter.initCenterData(mType);

    }

    @Override
    public void showCenterData(List<FindCenterItemBean> findCenterItemBeanList) {
        if (mAdapter == null) {
            mAdapter = new FindCenterRecyAdapter(findCenterItemBeanList);
            recyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.setNewData(findCenterItemBeanList);
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {

    }
}
